package com.project.common.domain;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.project.common.domain.po.BaseVersionPO;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;

import java.lang.reflect.InvocationTargetException;

/**
 * BaseDomainConvert
 *
 */
@Slf4j
@Data
public class BaseDoConvert extends Convert {
    @JsonIgnore
    private Integer delState = 0;

    public void convertTo(Object destinationType) {
        Assert.notNull(destinationType, "参数不能为NULL");
        getConvert().map(this, destinationType);
    }

    /**
     * 转对象
     *
     * @param <T>
     * @return T
     */
    public <T> T convertTo(Class<T> t) {
        Assert.notNull(t, "参数不能为NULL");
        T result = null;
        try {
            result = t.getDeclaredConstructor().newInstance();
        } catch (InstantiationException e) {
            log.error("转换异常" + e);
        } catch (IllegalAccessException e) {
            log.error("转换异常" + e);
        } catch (InvocationTargetException e) {
            log.error("转换异常" + e);
        } catch (NoSuchMethodException e) {
            log.error("转换异常" + e);
        }
        if (null == result) {
            throw new RuntimeException("转换异常");
        }
        getConvert().map(this, result);
        return result;
    }

    /**
     * 转PO
     *
     * @param t
     * @param <T>
     * @return
     */
    public <T extends BaseVersionPO> T convertToPo(Class<T> t) {
        Assert.notNull(t, "参数不能为NULL");
        return convertTo(t);
    }

}
